
define([], function (require, exports) {
    exports.ready = function (hashCode, data, cite) {
        // 注册该功能要用到的数据字典
        yufp.lookup.reg('DATA_STS,SYS_TYPE,SEX_TYPE,CONTACT_TYPE');
        // 创建virtual model
        yufp.custom.vue({
            el: '#merchantManagement',
            // 以m_开头的属性为UI数据不作为业务数据，否则为业务数据
            data: function () {
                var me = this;
                    return {
                        judgeUser:true,
                        options1:[],
                        options2:[],
                        merchantInfo:{
                            isDocument:'',
                            sceneType:'',
                        },
                        rules: {
                            merchantName: [
                                {required: true, message: '必填项', trigger: 'blur'}
                            ],
                            contactPerson: [
                                {required: true, message: '必填项', trigger: 'blur'}
                            ],
                            businessScope: [
                                {required: true, message: '必填项', trigger: 'blur'}
                            ],
                            address: [
                                {required: true, message: '必填项', trigger: 'blur'}
                            ],
                            bizLicNo: [
                                {required: true, message: '必填项', trigger: 'blur'}
                            ],
                            ctfNo: [
                                {required: true, message: '必填项', trigger: 'blur'}
                            ],
                            contactType: [
                                {required: true, message: '必填项', trigger: 'blur'}
                            ],
                            contactPhone: [
                                {required: true, validator: yufp.validator.mobile, message: "请输入正确的11位手机号", trigger: 'blur' }
                            ],
                        },
                        merchantData:{condition: JSON.stringify({
                                userId: yufp.session.userId,
                                orgId: yufp.session.user.org.code,
                                roleId:yufp.session.user.roles[0].code,
                            })},
                        funcDataUrl:backend.msmFrontWebService + '/api/MerchantManagementResource/getMerchantManagementInfo',
                        uploadData:{condition: JSON.stringify({
                                userId: yufp.session.userId,
                                orgId: yufp.session.orgId,
                            })},
                        dialogFormVisible: false,
                        dialogStatus: '',
                        saveBtnShow:'',
                        formDisabled: false,
                        loading: false,
                };
            },
            created:function (){
                this.judgeUserFn()
            },
            // 钩子
            mounted: function () {
                this.getGridName()
            },
            methods: {
                judgeUserFn:function (){
                    var me =this;
                    const roleId = yufp.session.user.roles[0].code;
                    const orgId = yufp.session.user.org.code;
                    if (roleId ==="R0008" || roleId === "R0007" || roleId === "R001"|| orgId=== "070667XN0" || orgId==="070667XN1" ||orgId==="070667860" ||orgId==="070667XN3"){
                        me.judgeUser= true;
                    }
                    else {
                        me.judgeUser= false;
                        me.getSecondGridName();
                    }
                },
                changeMerchantId:function (){
                    this.$refs.merchantInfoSearch.formdata.secondGridId = '';
                    this.getSecondGridName()
                },
                getGridName:function (){
                    var me =this;
                    yufp.service.request({
                        url: backend.msmFrontWebService + '/api/CustCenter/getGridName',
                        method: 'get',
                        data: {
                            condition: JSON.stringify({
                                userId: yufp.session.userId,
                                orgId: yufp.session.user.org.code,
                                roleId:yufp.session.user.roles[0].code,
                            })
                        },
                        callback: function (code, message, response) {
                            me.options1 = response.data
                        }
                    });
                },
                getSecondGridName:function (){
                    var me =this;
                    yufp.service.request({
                        url: backend.msmFrontWebService + '/api/CustCenter/getSecondGridName',
                        method: 'get',
                        data: {
                            condition: JSON.stringify({
                                userId: yufp.session.userId,
                                orgId: yufp.session.user.org.code,
                                roleId:yufp.session.user.roles[0].code,
                                preGrid: me.judgeUser=== true ? me.$refs.merchantInfoSearch.formdata.orgId : ''
                            })
                        },
                        callback: function (code, message, response) {
                            me.options2 = response.data
                        }
                    });
                },
                searchFn: function () {
                    var me = this;
                    console.log(me.$refs.merchantInfoSearch.formdata.secondGridId)
                    var param = {
                        condition: JSON.stringify({
                            merchantName: me.$refs.merchantInfoSearch.formdata.merchantName,
                            contactPerson: me.$refs.merchantInfoSearch.formdata.contactPerson,
                            secondGridId: me.$refs.merchantInfoSearch.formdata.secondGridId
                        })
                    };
                    me.$refs.merchantTable.remoteData(param);
                },
                getMerchantInformation:function (){
                    var _this =this;
                    if (this.$refs.merchantTable.selections.length !== 1) {
                        this.$message({message: '请选择一个选项', type: 'warning'});
                        return;
                    }
                    _this.switchStatus('edit', true);
                    yufp.service.request({
                        url: backend.msmFrontWebService + '/api/MerchantManagementResource/getMerchantInformation',
                        method: 'get',
                        data: {
                            condition: JSON.stringify({
                                id:this.$refs.merchantTable.selections[0].id
                            })
                        },
                        callback: function (code, message, response) {
                            if (code === 0) {
                                _this.merchantInfo.merchantName = response.data.merchantName;
                                _this.merchantInfo.contactPerson = response.data.contactPerson;
                                _this.merchantInfo.contactPhone = response.data.contactPhone;
                                _this.merchantInfo.bizLicNo = response.data.bizLicNo;
                                _this.merchantInfo.ctfNo = response.data.ctfNo;
                                _this.merchantInfo.gndrCd = response.data.gndrCd;
                                _this.merchantInfo.contactType = response.data.contactType;
                                _this.merchantInfo.businessScope = response.data.businessScope;
                                _this.merchantInfo.address = response.data.address;
                            } else {
                            }
                        }
                    });
                },
                deleteMerchantManagement:function (){
                    var _this = this;
                    //删除时的选项校验
                    if (_this.$refs.merchantTable.selections.length === 0) {
                        _this.$message({message: '请选择至少一个选项', type: 'warning'});
                        return;
                    }
                    var merchantNoArray =[];
                    for (var i=0;i<_this.$refs.merchantTable.selections.length;i++){
                        merchantNoArray.push(_this.$refs.merchantTable.selections[i].id)
                    }
                    yufp.service.request({
                        url: backend.msmFrontWebService + '/api/MerchantManagementResource/deleteMerchantManagement',
                        method: 'get',
                        data: {
                            condition: JSON.stringify({
                                userId: yufp.session.userId,
                                orgId: yufp.session.orgId,
                                merchantNo:merchantNoArray,
                            })
                        },
                        callback: function (code, message, response) {
                            _this.$refs.merchantTable.remoteData();
                            if (response !== 0) {
                                _this.$message({message: '删除成功', type: 'success'});
                                //获取上级网格名称和ID
                            } else {
                                console.info("失败");
                            }
                        }
                    });
                },
                addMerchantFn:function (){
                    var _this = this;
                    _this.restInputValue();
                    _this.switchStatus('add', true);
                },
                saveEditFunc: function () {
                    let _this=this;
                    let validate = false;
                    _this.$refs.merchantForm.validate(function (valid) {
                        validate = valid;
                    });
                    if (!validate) {
                        this.$message({message: '打*为必填项', type: 'warning'});
                        return;
                    }
                    yufp.service.request({
                        url:_this.dialogStatus ==='edit' ? backend.msmFrontWebService + '/api/MerchantManagementResource/updateMerchantManagement' : backend.msmFrontWebService + '/api/MerchantManagementResource/addMerchantManagement',
                        method: 'get',
                        data: {
                            condition: JSON.stringify({
                                id:_this.dialogStatus ==='edit' ? this.$refs.merchantTable.selections[0].id :'' ,
                                merchantName:_this.merchantInfo.merchantName,
                                contactPerson:_this.merchantInfo.contactPerson,
                                contactPhone:_this.merchantInfo.contactPhone,
                                bizLicNo:_this.merchantInfo.bizLicNo,
                                ctfNo:_this.merchantInfo.ctfNo,
                                gndrCd:_this.merchantInfo.gndrCd,
                                contactType:_this.merchantInfo.contactType,
                                businessScope:_this.merchantInfo.businessScope,
                                address:_this.merchantInfo.address,
                            })
                        },
                        callback: function (code, message, response) {
                            _this.restInputValue();
                            if(_this.dialogStatus === 'edit' && code ===0){
                                _this.$refs.merchantTable.remoteData();
                                _this.$message({message: '更新商户户成功', type: 'success'});
                            }
                            else if (_this.dialogStatus === 'add' && code === 0) {
                                let integralCode = '11';
                                yufp.util.addIntegral(integralCode)
                                _this.$refs.merchantTable.remoteData();
                                _this.$message({message: '新增商户成功', type: 'success'});
                            } else {
                                _this.$message({message: '新增商户失败,请检查是否插入了重复的客户', type: 'warning'});
                            }
                        }
                    });
                },
                //每次执行显示操作，新增操作执行清空，修改执行查询，
                restInputValue:function (){
                    var _this = this;
                    _this.dialogFormVisible = false;
                    _this.$nextTick(function () {
                        if (_this.$refs.merchantForm){
                            _this.$refs.merchantForm.resetFields();
                        }
                    });
                },
                switchStatus: function (viewType, editable) {
                    var _this = this;
                    _this.dialogStatus = viewType;
                    _this.saveBtnShow = editable;
                    _this.dialogFormVisible = true;
                    _this.formDisabled = !editable;
                },
            }
        });
    };

    // 消息处理
    exports.onmessage = function (type, message) { };

    // page销毁时触发destroy方法
    exports.destroy = function (id, cite) {};
});
